/* Team Directories
 *
 * You are free to do whatever you want with this code. Frankly we don't give a damn.
 * 
 *  
 * Copyright GNU General Public License v3
 */

/*
    This class will load and save plugins, etc ... also this class will be responsible for creating
 * plugins ... we can add plugins using addplugins, and run them as we see fit.
 
 */

package StudyBuddy;
import java.io.*;
import java.util.*;

/**
 *
 * @author Gabriel
 */
public class PluginCollection {

ArrayList<Plugin> collection = new ArrayList<Plugin>();
// Expandable Array of plugins - added to plugin collection
// This allows an indefinete number of plugins to be added to the collection

    public void PluginCollection(){
        
        // Empty Constructor
        
    }
    
    public void writeListToFile(){
        
        /*Writes a list to the file - this saves the current state of plugin collection
         * and puts it in a file.
         * 
         * In this implementation of the Plugin Loader, ONLY the locations of the plugins
         * are stored to a file. The file is essensialy a list of the plugin's locations
         * in the user's local directories. Each new line corrosponds to a new plugin
         */
        
        try {
            
            // Writes to a file named outfilename
            // We assume such a file exists
            
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            for(int i =0; i<collection.size(); i++){
                System.out.println(collection.get(i).toString());
                out.write(collection.get(i).toString() + "\n");
            }
            out.close();
           
        } 
        
        
        catch (IOException e) {
        }
        
    }
    
    
    public void loadListFromFile() throws Exception{
        
        /*Loads a list from a file. We have to save the plugins in the collection so
         * the user doesn't have to load it again each time he/she starts the program
         * Note that if there are any existing plugins in plugincollection
         * it is cleared to prevent unecessary accumilation of files.
         * 
         * In this implementation of the Plugin Loader, ONLY the locations of the plugins
         * are stored to a file.The file is essensialy a list of the plugin's locations
         * in the user's local directories. Each new line corrosponds to a new plugin
         */
        
        try {
            collection.clear(); // First clears the collection ... if there's anything in it.
            
            // Now reads from a file named outfilename
            BufferedReader in = new BufferedReader(new FileReader("outfilename"));
            String str = "";
            while (str != null) {
                str = in.readLine();
                // Keeps reading every single line in the String.
 //               System.out.println(str);
                if (str != null){
                    System.out.println(str);
                    Plugin temp = new Plugin(new File(str));
                    temp.loadPlugin();
                    System.out.println(temp.toString());
                    collection.add(temp);
                }
            }
            in.close();
        } 
        
        catch (IOException e) {
            
            // If the file does not exist, write a blank file.
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            out.close();
        }
        
    }
        
    public void removePlugin(int index){
        
        /** Removes a plugin from the plugin collection
         * if the plugin collection is empty, it returns. This is done to avoid having to catch
         * a null pointer exception.
         */
        
        if(index >= 0 && index < collection.size()){
            collection.remove(index);
        }
        
    }
       
    public void addPlugin(File filename) throws Exception{
        
        /* Adds a plugin to the plugin collection
         * If the plugins are not added correctly for whatever reason, exceptions are thrown.
         * Common Exceptions Thrown Are
         * MalformedURLException
         * ClassNotFoundException (if the file is not a .class file)
         * IOException (general read/write errors, possibly caused by tampering of the system)
         * InstantiationException (Class is not loadable)
         * IllegalAccessAcception (Class may be in use)
         * NotRunnableException (thrown if class is not of type runnable)
         * DoubleBuddyException (thrown if the same buddy is loaded twice)
         */
        
        for(int i =0; i<collection.size(); i++){
            
            if(collection.get(i).toString().equals(filename.toString())){
                
                // If the user attempts to load two buddies from the same location
                // A new DoubleBuddyException is thrown.
                
                throw new DoubleBuddyException();
            
            }
            
        }
        
        Plugin temp = new Plugin(filename);
        temp.loadPlugin();
        
              
        collection.add(temp);
        
        
    }
    
    
    public String getPluginName(int index){
        
         /** Description: Returns the plugin name based on the index;
         * If the Index is invalid it returns null. This is done to avoid a nullpointer
         * Exception
         */
        
        if(index >= 0 && index < collection.size()){
            return collection.get(index).getName();
        }
        
        return null;
        
    }
    
    public void runPlugin(int index){
        
         /** Description: Runs the currrent plugin based on it's index by calling runplugin();
         * If the Index is invalid it returns. This is done to avoid a nullpointer
         * Exception
         */
        
        if(index >= 0 && index < collection.size()){
            collection.get(index).runplugin();
        }
                
    }
 
    
    public Plugin getPlugin(int index){
        
        /** Description: Returns a pointer to the currrent plugin based on it's index
         * If the Index is invalid it returns null. This is done to avoid a nullpointer
         * Exception
         */
        
        
        if(index >= 0 && index < collection.size()){
            return collection.get(index);
        }
        
        return null;
                
    }
    
    public int getSize(){
        
        /** Returns the size of the plugin collection
         *  If the collection is Empty, returns 0
         */
        return collection.size();
        
    }
    
    /*NOTE THESE FUNCTIONS ARE NOT WORKING PLEASE IGNORE*/
    /*NOTE THESE FUNCTIONS ARE NOT WORKING PLEASE IGNORE*/
    
    /*
    public void saveGameToFile(){
        
        for(Iterator i = collection.iterator(); i.hasNext();){
            ((Plugin)i.next()).saveGameToFile();

        }
        
    }
    
    public void loadSavedGameFromFile(){
        
        for(Iterator i = collection.iterator(); i.hasNext();){
            ((Plugin)i.next()).loadSavedGameFromFile();    
        }
        
       
    }
    */
}
